#pragma once

#include "parentpanel.h"
#include "undomanager.h"
#include "bitmap.h"
#include "imageclip.h"

/** the root panel that holds "global" variables, without being global */
class MasterPanel : public ParentPanel
{
public:
	static const int 
		COLOR_BG = 0,		//0x888888,
		COLOR_SELECTED = 1,	//0xaaaaaa,
		COLOR_ACTIVE = 2,	//0xcccccc,
		COLOR_PUSHED = 3,	//0x555555,
		NUM_COLORS = 4;
protected:
	UndoManager m_undo;
	Uint32 m_colors[NUM_COLORS];
	BitMaP * m_icons;
	SDL_Surface * m_defaultFontSurface;
	TemplateVector<SRect> m_dirtyRectangle;
	bool m_running;
public:
	void setRunning(const bool & a_running)
	{
		m_running = a_running;
	}
	bool isRunning(){return m_running;}
	TemplateVector<SRect> * getDirtyRectangles(){return &m_dirtyRectangle;}

	ImageClip getImageClip(const int & a_index)
	{
		int s = m_icons->getHeight();
		return ImageClip(m_icons, SRect(s*a_index, 0, s, s));
	}

	Uint32 getColor(const int & a_colorIndex){return m_colors[a_colorIndex];}

	void init()
	{
		m_running = true;
		m_colors[COLOR_BG] = 0xaaaaaa;
		m_colors[COLOR_SELECTED] = 0x888888;
		m_colors[COLOR_ACTIVE] = 0xcccccc;
		m_colors[COLOR_PUSHED] = 0x555555;
		// TODO magic strings
		if(!m_icons)
		{
			m_icons = BitMaP::load("sprites/tools.bmp");
		}
		if(!m_defaultFontSurface)
		{
			//m_defaultFontSurface = VSDL_loadImage("sprites/ascii.bmp");
			m_defaultFontSurface = VSDL_loadImage("sprites/arial.bmp");
			if(m_defaultFontSurface)
			{
				// TODO magic numbers...
				SDL_SetColorKey(m_defaultFontSurface, SDL_SRCCOLORKEY, SDL_MapRGB(
					m_defaultFontSurface->format, 255, 0, 255));
				//VSDL_setDefaultMonospaceFont(m_defaultFontSurface, SPoint(8,12), 0, 256);
				VSDL_setDefaultVariableWidthFont(m_defaultFontSurface, ' ', 95);
			}
		}
	}

	MasterPanel():m_icons(0),m_defaultFontSurface(0)
	{
		init();
	}

	UndoManager * getUndoManager(){return &m_undo;}

	void release()
	{
		if(m_icons)m_icons->release();
		{
			m_icons = 0;
		}
		if(m_defaultFontSurface)SDL_FreeSurface(m_defaultFontSurface);
		{
			m_defaultFontSurface = 0;
		}
		m_undo.release();
		ParentPanel::release();
	}

	bool handleEvent(const SDL_Event & e)
	{
		bool handled = false;
		switch(e.type)
		{
		case SDL_KEYDOWN:
			switch(e.key.keysym.sym)
			{
			case SDLK_z:
				if(e.key.keysym.mod & KMOD_CTRL)
				{
					handled = true;
					bool undone = getUndoManager()->undo();
					printf("undo! %d\n", undone);
					setDirty(0);
				}
				break;
			case SDLK_y:
				if(e.key.keysym.mod & KMOD_CTRL)
				{
					handled = true;
					bool redone = getUndoManager()->redo();
					printf("redo! %d\n", redone);
					setDirty(0);
				}
				break;
			}
		}
		if(!handled)
			handled = ParentPanel::handleEvent(e);
		return handled;
	}

	void draw(SDL_Surface * a_surface);

	bool isDirty();

	virtual bool isMasterPanel(){return true;}

	/** clears the dirty rectangles of the root UI panel */
	void clearDirtyRectangles()
	{
		getDirtyRectangles()->clear();
	}
};
